package com.addplus.townmall.server.web.consumer.action.websocket;


import com.addplus.townmall.server.api.model.base.MessageReturn;
import com.addplus.townmall.server.api.model.base.MessageWebParam;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

/**
 * 类名: WebScoketRequestAction
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2017/12/21 下午7:44
 * @description 类描述: websocket请求通用类
 */
@Controller
public class WebSocketRequestAction  {


    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    /**
     * 方法名: broadcastReturnAll
     *
     * @author zhangjiehang
     * @version V1.0
     * @date 2017/12/22 下午4:39
     * @description 方法描述:请求后把对应信息广播到所有订阅了该频道的用户
     */
    @MessageMapping("/websocket/sanber/institution/broadcast/allaccount")
    public void broadcastReturnAll(MessageWebParam messageWebParam) {
        MessageReturn messageReturn = new MessageReturn();
        simpMessagingTemplate.convertAndSend(messageWebParam.getSubscribe(), messageReturn);
    }

    /**
     * 方法名: broadcastReturnSingle
     *
     * @author zhangjiehang
     * @version V1.0
     * @date 2017/12/22 下午4:39
     * @description 方法描述:请求后定向推给对应的用户，定向推送的频道必须要是user开头
     */
    @MessageMapping("/websocket/sanber/institution/broadcast/singleaccount")
    public void broadcastReturnSingle(MessageWebParam messageWebParam) {
        MessageReturn messageReturn = new MessageReturn();
        simpMessagingTemplate.convertAndSendToUser(messageWebParam.getUserId().toString(), messageWebParam.getSubscribe(), messageReturn);
    }

    @MessageMapping("/websocket/sanber/strategyResearch/python")
    public void sendTempResult(MessageWebParam messageWebParam) {
        MessageReturn messageReturn = new MessageReturn();
        messageReturn.setTimestamp(System.currentTimeMillis());
        messageReturn.setPath(messageWebParam.getPath());
    }

    private String generateJsonContent(MessageWebParam messageWebParam) {
        JSONObject json = new JSONObject();
        json.put("strategy_id", messageWebParam.getParams().get("strategyId"));
        json.put("start_date", messageWebParam.getParams().get("startDate"));
        json.put("end_date", messageWebParam.getParams().get("endDate"));
        json.put("frequency", messageWebParam.getParams().get("timeInterval"));
        json.put("start_catpital", messageWebParam.getParams().get("startCatpital"));
        json.put("standard_symbol", messageWebParam.getParams().get("standardSymbol"));
        json.put("commission_rate", messageWebParam.getParams().get("commissionRate"));
        json.put("slippage", messageWebParam.getParams().get("slippage"));
        json.put("matching_type", messageWebParam.getParams().get("matchingType"));
        json.put("run_type", messageWebParam.getParams().get("runType"));
        json.put("back_test_id", messageWebParam.getParams().get("back_test_id"));
        json.put("code", messageWebParam.getParams().get("pythonCode"));
        return json.toJSONString();
    }

}
